import { BusinessItemListInfo } from "../../types/business";
import { clone, get, pick, set } from "lodash";
import Dialog from "../Dialog";
import { Form, Slider } from "antd";
import Upload from "../upload";
import request from "../../utils/request";

export const pictureDialog = (data?: BusinessItemListInfo) => {
  const isModify = !!data;
  //@ts-ignore
  let newData: BusinessItemListInfo = clone(data);
  const initialData = {
    media_url: "",
    threshold: 0.8,
    score: 1
  };
  if (isModify) {
    initialData.media_url = get(data, "media_info[0].media_url", "");
    initialData.threshold = get(data, "threshold", 0);
    initialData.score = get(data, "score", 0);
  }
  return new Promise((resolve, reject) => {
    Dialog({
      title: isModify ? "编辑图像" : "新增图像",
      content: (
        <Form initialValues={initialData}>
          <Form.Item label="图片" name="media_url">
            <Upload listType="picture-card" maxCount={1} />
          </Form.Item>
          <Form.Item label="阈值" name="threshold">
            <Slider min={0} max={1} step={0.01} />
          </Form.Item>
          <Form.Item label="分数" name="score">
            <Slider min={0} max={10} />
          </Form.Item>
        </Form>
      ),
      onOk: async (values, hidden) => {
        const info = await request.post("/hyqz/validate/pic", {
          type: 1,
          url: values.media_url,
        });
        if (!isModify) {
          newData = {
            // @ts-ignore
            media_info: [{ media_url: "" }],
          };
        }
        Object.assign(newData, pick(values, ["score"]));
        newData.threshold = get(values, "threshold", 0);
        set(newData, "media_info[0].media_url", values.media_url);
        set(newData, "media_info[0]", {
          ...get(newData, "media_info[0]", {}),
          ...info,
        });
        set(newData, "media_info", clone(newData.media_info));

        resolve(newData);
        hidden();
      },
      onCancel: reject,
    });
  });
};
